package com.landiao.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.landiao.common.pojo.CategoryInfo;
import com.landiao.common.pojo.EmployeeReport;
import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.PerformanceHisInfo;
import com.landiao.common.pojo.Querier;
import com.landiao.common.utils.DateUtil;
import com.landiao.entities.ItemCategory;
import com.landiao.mapper.EmployeeMapper;
import com.landiao.mapper.ItemCategoryMapper;

@Service
public class EmployeeReportService extends BaseService<EmployeeReport,String>{
	
	@Autowired
	private ItemCategoryMapper itemCategoryMapper;
	@Autowired
	private EmployeeMapper employeeMapper;
	@Autowired
	private OrderScheduleService oschService;
	

	public List<ItemCategory> getCategoryList() {
		// TODO Auto-generated method stub
		return itemCategoryMapper.getItemCategorys(null);
	}

	public Page<EmployeeReport> queryByPage(Querier querier, EmployeeReport t) {
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		Page<EmployeeReport> page = new Page<EmployeeReport>(querier);
    	List<EmployeeReport> list = employeeMapper.getReportList(t);
    	//获取门店总业绩和门店外卖业绩
    	String shopresults = employeeMapper.getShopResults(t);
    	Double shopresultsNum = Double.parseDouble(shopresults==null?"0.0":shopresults.toString());
    	String shopOutResults = employeeMapper.getShopOutResults(t);
    	Double shopOutresultsNum =  Double.parseDouble(shopOutResults==null?"0.0":shopOutResults.toString());
    	//获取项目类型
    	List<ItemCategory> items = this.getCategoryList();
    	
    	if(items!=null&&items.size()>0){
    		for(int i=0;i<items.size();i++){
    			//通过项目类别id、门店id、开始日期、结束日期获取该门店该类别的金额总和
    			t.setCategoryId(items.get(i).getItemCategoryId());
    			String amount = employeeMapper.getCategoryAmount(t);//类别金额总和 用于计算该类别各个员工所占比例
    			Double AmountNum = 0.0;
    			if(amount!=null){
    				AmountNum = Double.parseDouble(amount);
    			}
    			for(int k=0;k<list.size();k++){
    				//计算员工业绩 占门店业绩比例 外卖业绩占门店外卖业绩比例
    				Double results1 = Double.parseDouble(list.get(k).getResults1()!=null?list.get(k).getResults1():"0.0");
					Double results2 = Double.parseDouble(list.get(k).getResults2()!=null?list.get(k).getResults2():"0.0");
					Double results = results1+results2;
					Double resultsPercent = 0.00;
    				if(shopresultsNum>0&&results>0){
    					resultsPercent = results/shopresultsNum;
    				}
    				list.get(k).setResults(df1.format(results));
    				list.get(k).setResultsPercent(df2.format(resultsPercent));
    				
    				Double outResults = Double.parseDouble(list.get(k).getOutResults()!=null?list.get(k).getOutResults():"0.0");
					Double outResultsPercent = 0.00;
    				if(shopOutresultsNum>0&&outResults>0){
    					outResultsPercent = outResults/shopOutresultsNum;
    				}
    				list.get(k).setOutResultsPercent(df2.format(outResultsPercent));
    				
    				//计算list中的值
    				t.setEmployeeId(list.get(k).getEmployeeId());
    				CategoryInfo cate = employeeMapper.getCategoryInfo(t);
    				if(cate==null){
    					cate = new CategoryInfo();
    				}
    				Double num = Double.parseDouble(cate.getAmount()==null?"0.0":cate.getAmount());
    				Double percent = 0.00;
    				if(AmountNum>0){
    					percent = num/AmountNum;
    				}
    				cate.setPercent(df2.format(percent));
    				
    				if(list.get(k).getList()==null){
    					List<CategoryInfo> cateList =new ArrayList<CategoryInfo>();
    					cateList.add(cate);
    					list.get(k).setList(cateList);
    				}else{
    					list.get(k).getList().add(cate);
    				}
    			}
    		}
    	}
    	page.setRows(list);
    	page.setPageSize(list.size());
        return page;
	}
	public List<EmployeeReport> getEmployeeReport(EmployeeReport t) {
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
    	List<EmployeeReport> list = employeeMapper.getReportList(t);
    	
    	//添加汇总信息
    	EmployeeReport allReoprt = new EmployeeReport();
		Double allresult=0.0;
		Double alloutResult =0.0;
		Double allresultsPercent=0.00;
		Double alloutResultsPercent=0.00;
		Integer allcustomTotle = 0;
		Integer allhotCustom = 0;
		Integer allstrangerCustom = 0;
		List<CategoryInfo> allcateList =new ArrayList<CategoryInfo>();
    	
    	//获取门店总业绩和门店外卖业绩
    	String shopresults = employeeMapper.getShopResults(t);
    	Double shopresultsNum = Double.parseDouble(shopresults==null?"0.0":shopresults.toString());
    	String shopOutResults = employeeMapper.getShopOutResults(t);
    	Double shopOutresultsNum =  Double.parseDouble(shopOutResults==null?"0.0":shopOutResults.toString());
    	Double shopItemresultsNum = shopresultsNum-shopOutresultsNum;
    	System.out.println("门店业绩shopresultsNum="+shopresultsNum+"  门店外面业绩="+shopOutresultsNum+" 门店项目业绩="+shopItemresultsNum);
    	//获取项目类型
    	List<ItemCategory> items = this.getCategoryList();
    	
    	if(items!=null&&items.size()>0){
    		for(int i=0;i<items.size();i++){
    			//通过项目类别id、门店id、开始日期、结束日期获取该门店该类别的金额总和
    			t.setCategoryId(items.get(i).getItemCategoryId());
    			String amount = employeeMapper.getCategoryAmount(t);//类别金额总和 用于计算该类别各个员工所占比例
    			Double AmountNum = 0.0;
    			if(amount!=null){
    				AmountNum = Double.parseDouble(amount);
    			}
    			for(int k=0;k<list.size();k++){
    				//计算员工业绩 占门店业绩比例 外卖业绩占门店外卖业绩比例
    				Double results1 = Double.parseDouble(list.get(k).getResults1()!=null?list.get(k).getResults1():"0.0");
					Double results2 = Double.parseDouble(list.get(k).getResults2()!=null?list.get(k).getResults2():"0.0");
					Double results = results1+results2;
					Double resultsPercent =0.00;
    				if(shopresultsNum>0&&results>0){
    					resultsPercent = results/shopItemresultsNum;
    				}
    				list.get(k).setResults(df1.format(results));
    				list.get(k).setResultsPercent(df2.format(resultsPercent));
    				
    				allresult+=results;
    				allresultsPercent+=resultsPercent;
    				
    				Double outResultsPercent = 0.00;
    				Double outResults = Double.parseDouble(list.get(k).getOutResults()!=null?list.get(k).getOutResults():"0.0");
    				if(shopOutresultsNum>0&&outResults>0){
    					outResultsPercent = outResults/shopOutresultsNum;
    				}
    				list.get(k).setOutResultsPercent(df2.format(outResultsPercent));
    				
    				alloutResult+=outResults;
    				alloutResultsPercent+=outResultsPercent;
    				allcustomTotle+=Integer.parseInt(list.get(k).getCustomTotle());
    				allhotCustom+=Integer.parseInt(list.get(k).getHotCustom());
    				allstrangerCustom+=Integer.parseInt(list.get(k).getStrangerCustom());
    				//计算list中的值
    				t.setEmployeeId(list.get(k).getEmployeeId());
    				CategoryInfo cate = employeeMapper.getCategoryInfo(t);
    				if(cate==null){
    					cate = new CategoryInfo();
    				}
    				Double percent=0.00;
    				Double num = Double.parseDouble(cate.getAmount()==null?"0.0":cate.getAmount());
    				if(AmountNum>0&&num>0){
    					percent = num/AmountNum;
    				}
    				cate.setAmount(df1.format(num));
    				cate.setPercent(df2.format(percent));
    			
    				if(list.get(k).getList()==null){
    					List<CategoryInfo> cateList =new ArrayList<CategoryInfo>();
    					cateList.add(cate);
    					list.get(k).setList(cateList);
    				}else{
    					list.get(k).getList().add(cate);
    				}
    				if(allcateList.size()>i){
    					CategoryInfo cate2 =  new CategoryInfo();
    					Integer allnum = Integer.parseInt(cate.getNum())+Integer.parseInt(allcateList.get(i).getNum());
    					Double allAmount = num+Double.parseDouble(allcateList.get(i).getAmount()==null?"0.0":allcateList.get(i).getAmount());
    					Double allpercent = percent+Double.parseDouble(allcateList.get(i).getPercent().substring(0, allcateList.get(i).getPercent().length()-1))/100;
    					cate2.setNum(allnum.toString());
    					cate2.setAmount(df1.format(allAmount));
    					cate2.setPercent(df2.format(allpercent));
    					allcateList.set(i, cate2);
    				}else{
    					allcateList.add(cate);
    				}
    			}
    		}
    		allresult=allresult/items.size();
    		alloutResult =alloutResult/items.size();
    		allresultsPercent=allresultsPercent/items.size();
    		alloutResultsPercent=alloutResultsPercent/items.size();
    		allcustomTotle = allcustomTotle/items.size();
    		allhotCustom =allhotCustom/items.size();
    		allstrangerCustom = allstrangerCustom/items.size();
    	}
    	allReoprt.setEmployeeName("汇总");
    	allReoprt.setResults(df1.format(allresult));
    	allReoprt.setResultsPercent(df2.format(allresultsPercent));
    	allReoprt.setOutResults(df1.format(alloutResult));
    	allReoprt.setOutResultsPercent(df2.format(alloutResultsPercent));
    	allReoprt.setCustomTotle(allcustomTotle.toString());
    	allReoprt.setHotCustom(allhotCustom.toString());
    	allReoprt.setStrangerCustom(allstrangerCustom.toString());
    	allReoprt.setList(allcateList);
    	list.add(allReoprt);
        return list;
	}

	public List<EmployeeReport> getServiceReport(EmployeeReport t) {
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		List<EmployeeReport> results = null;
		//用于汇总
		EmployeeReport allreport = new EmployeeReport();
		Double allamount = 0.0;//总金额
		Integer allnum = 0;//总数量
		Integer allMCustom = 0;//男客数量
		Double allMAmount = 0.0;//男客金额
		Integer allWCustom = 0;//女客数量
		Double allWAmount = 0.0;//女客金额
		Double allavgPrice = 0.0;//均价
		Double allMPercent=0.00;//男客占百分比
		Double allWPercent=0.00;//女客占百分比
		Double allmamountPercent=0.00;//男客金额占百分比
		Double allwamountPercent=0.00;//女客金额占百分比
		Double allPercent=0.00;//占百分比
		//获取门店项目业绩
    	String shopserviceresults = employeeMapper.getShopServiceResults(t);
    	Double shopserviceresultsNum = Double.parseDouble(shopserviceresults==null?"0.0":shopserviceresults.toString());
		//获取项目类型
    	List<EmployeeReport> items = employeeMapper.getEmployeeReportCategory(t);
    	if(items!=null&&items.size()>0){
    		results = new ArrayList<EmployeeReport>();
    		for(int i=0;i<items.size();i++){
    			EmployeeReport item = items.get(i);
    			//数据转换
    			fillDate(item,shopserviceresultsNum);
    			item.setType(true);
    			results.add(item);
    			
    			allamount += Double.parseDouble(item.getAmount());//总金额
    			allnum += Integer.parseInt(item.getNum());//总数量
    			allMCustom += Integer.parseInt(item.getMcustom());//男客数量
    			allMAmount += Double.parseDouble(item.getMamount());//男客金额
    			allWCustom += Integer.parseInt(item.getWcustom());//女客数量
    			allWAmount += Double.parseDouble(item.getWamount());//女客金额
    			allavgPrice += Double.parseDouble(item.getAvgPrice());//均价
    			allPercent+=Double.parseDouble(item.getPercent().substring(0, item.getPercent().length()-1))/100;//占百分比
    			
    			//通过categoryId获取该类别项目的信息
    			t.setCategoryId(item.getCategoryId());
    			List<EmployeeReport> items2 = employeeMapper.getEmployeeReportByCategoryId(t);
    			if(items2 !=null&&items2.size()>0){
    				for(int j=0;j<items2.size();j++){
    					EmployeeReport item2 = items2.get(j);
    					fillDate(item2,shopserviceresultsNum);
    	    			item2.setType(false);
    	    			results.add(item2);
        			}
    			}
    			
    		}
    	}
    	allreport.setName("汇总");
    	allreport.setNum(allnum.toString());
    	allreport.setAvgPrice(df1.format(allavgPrice));
    	allreport.setAmount(df1.format(allamount));
    	allreport.setPercent(df2.format(allPercent));
    	
    	if(allnum>0&&allMCustom>0){
    		allMPercent = Double.parseDouble(allMCustom.toString())/Double.parseDouble(allnum.toString());
    	}
    	if(allamount>0&&allWAmount>0){
    		allmamountPercent = allMAmount/allamount;
    	}
    	allreport.setMcustom(allMCustom.toString());
    	allreport.setMamount(df1.format(allMAmount));
    	allreport.setMpercent(df2.format(allMPercent));
    	allreport.setMamountPercent(df2.format(allmamountPercent));
    	
    	if(allnum>0&&allWCustom>0){
    		allWPercent = Double.parseDouble(allWCustom.toString())/Double.parseDouble(allnum.toString());
    	}
    	if(allamount>0&&allWAmount>0){
    		allwamountPercent = allWAmount/allamount;
    	}
    	allreport.setWcustom(allWCustom.toString());
    	allreport.setWamount(df1.format(allWAmount));
    	allreport.setWpercent(df2.format(allWPercent));
    	allreport.setWamountPercent(df2.format(allwamountPercent));
    	results.add(allreport);
    	
		return results;
	}
	private void fillDate(EmployeeReport item,Double shopserviceresultsNum ){
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		//数据转换
		Double amount = Double.parseDouble(item.getAmount());//总金额
		Double num = Double.parseDouble(item.getNum());//总数量
		Double MCustom = Double.parseDouble(item.getMcustom());//男客数量
		Double MAmount = Double.parseDouble(item.getMamount());//男客金额
		Double WCustom = Double.parseDouble(item.getWcustom());//女客数量
		Double WAmount = Double.parseDouble(item.getWamount());//女客金额
		Double avgPrice = 0.0;//均价
		Double MPercent=0.00;//男客占百分比
		Double WPercent=0.00;//女客占百分比
		if(num>0){
			avgPrice = amount/num;
			if(MCustom>0){
				MPercent = MCustom/num;
			}
			if(WCustom>0){
				WPercent = WCustom/num;
			}
		}
		item.setAvgPrice(df1.format(avgPrice));
		item.setMpercent(df2.format(MPercent));
		item.setWpercent(df2.format(WPercent));
		
		Double percent = 0.00;//百分比
		Double MAmountPercent = 0.00;//男客金额占百分比
		Double WAmountPercent = 0.00;//女客金额占百分比
		if(shopserviceresultsNum>0&&amount>0){
			percent = amount/shopserviceresultsNum;
		}
		item.setPercent(df2.format(percent));
		if(amount>0){
			if(MAmount>0){
				MAmountPercent = MAmount/amount;
			}
			if(WAmount>0){
				WAmountPercent = WAmount/amount;
			}
		}
		item.setMamountPercent(df2.format(MAmountPercent));
		item.setWamountPercent(df2.format(WAmountPercent));
	}

	public List<EmployeeReport> getOutReport(EmployeeReport t) {
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		List<EmployeeReport> results = null;
		//用于汇总
		EmployeeReport allreport = new EmployeeReport();
		Double allamount = 0.0;//总金额
		Integer allnum = 0;//总数量
		Double allavgPrice = 0.0;//均价
		Double allPercent=0.00;//占百分比
		//获取门店项目业绩
		String shopOutResults = employeeMapper.getShopOutResults(t);
    	Double shopOutresultsNum =  Double.parseDouble(shopOutResults==null?"0.0":shopOutResults.toString());
    	//获取商品分类信息
    	List<EmployeeReport> items = employeeMapper.getEmployeeReportMdseCategory(t);
    	if(items!=null&&items.size()>0){
    		results = new ArrayList<EmployeeReport>();
    		for(int i=0;i<items.size();i++){
    			EmployeeReport item = items.get(i);
    			//数据转换
    			fillDate2(item,shopOutresultsNum);
    			item.setType(true);
    			results.add(item);
    			
    			allamount += Double.parseDouble(item.getAmount());//总金额
    			allnum += Integer.parseInt(item.getNum());//总数量
    			allavgPrice += Double.parseDouble(item.getAvgPrice());//均价
    			allPercent+=Double.parseDouble(item.getPercent().substring(0, item.getPercent().length()-1))/100;//占百分比
    			
    			//通过categoryId获取该类别项目的信息
    			t.setCategoryId(item.getCategoryId());
    			List<EmployeeReport> items2 = employeeMapper.getEmployeeReportByMdseCategoryId(t);
    			if(items2 !=null&&items2.size()>0){
    				for(int j=0;j<items2.size();j++){
    					EmployeeReport item2 = items2.get(j);
    					fillDate2(item2,shopOutresultsNum);
    	    			item2.setType(false);
    	    			results.add(item2);
        			}
    			}
    			
    		}
    	}
    	allreport.setName("汇总");
    	allreport.setNum(allnum.toString());
    	allreport.setAvgPrice(df1.format(allavgPrice));
    	allreport.setAmount(df1.format(allamount));
    	allreport.setPercent(df2.format(allPercent));
    	results.add(allreport);
    	
		return results;
	}
	private void fillDate2(EmployeeReport item,Double shopOutresultsNum ){
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		//数据转换
		Double amount = Double.parseDouble(item.getAmount());//总金额
		Double num = Double.parseDouble(item.getNum());//总数量
		Double avgPrice = 0.0;//均价
		if(num>0){
			avgPrice = amount/num;
		}
		item.setAvgPrice(df1.format(avgPrice));
		Double percent = 0.00;//百分比
		if(shopOutresultsNum>0&&amount>0){
			percent = amount/shopOutresultsNum;
		}
		item.setPercent(df2.format(percent));
	}

	public List<EmployeeReport> getShopReport(EmployeeReport t) {
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		//获取门店统计信息
		List<EmployeeReport> items = employeeMapper.getShopReportList(t);
		//用于汇总数据
		EmployeeReport allReoprt = new EmployeeReport();
		Double allshopResult = 0.0;
		Double allshopItemResult=0.0;
		Double allshopOutResult =0.0;
		Double allcashPayAmount = 0.0;
		Double allcardPayAmount = 0.0;
		Double alipayPayAmount = 0.0;
		Double allonlinePayAmount = 0.0;
		Double allbalancePayAmount = 0.0;
		Double allbulkPayAmount = 0.0;
		Double shopPaidResults = 0.0;
		Integer allcustomTotle = 0;
		Integer allmcustom = 0;
		Integer allwcustom = 0;
		Integer allhotCustom = 0;
		Integer allstrangerCustom = 0;
		List<CategoryInfo> allcateList =new ArrayList<CategoryInfo>();
		if(items!=null&&items.size()>0){
			for(int i=0;i<items.size();i++){
				//门店总业绩
				Double shopAmount = Double.parseDouble(items.get(i).getShopResult());
				Double shopItemAmount =Double.parseDouble(items.get(i).getShopItemResult());
				allshopResult+=shopAmount;
				allshopItemResult+=shopItemAmount;
				allshopOutResult+=Double.parseDouble(items.get(i).getShopOutResult());
				allcashPayAmount+=Double.parseDouble(items.get(i).getCashPayAmount());
				allcardPayAmount+=Double.parseDouble(items.get(i).getCardPayAmount());
				alipayPayAmount+=Double.parseDouble(items.get(i).getAlipayPayAmount());
				allbulkPayAmount+=Double.parseDouble(items.get(i).getBulkPayAmount());
				allonlinePayAmount+=Double.parseDouble(items.get(i).getOnlinePayAmount());
				allbalancePayAmount+=Double.parseDouble(items.get(i).getBalancePayAmount());
				shopPaidResults+=Double.parseDouble(items.get(i).getShopPaidResults());
				allcustomTotle += Integer.parseInt(items.get(i).getCustomTotle());
				allmcustom += Integer.parseInt(items.get(i).getMcustom());
				allwcustom += Integer.parseInt(items.get(i).getWcustom());
				allhotCustom += Integer.parseInt(items.get(i).getHotCustom());
				allstrangerCustom += Integer.parseInt(items.get(i).getStrangerCustom());
				//获取项目类型
				//通过门店id 分别获取不同类别项目的信息
				t.setShopId(items.get(i).getShopId());
		    	List<EmployeeReport> items2 = employeeMapper.getEmployeeReportCategory(t);
		    	if(items2!=null&&items2.size()>0){
		    		List<CategoryInfo> cateList =new ArrayList<CategoryInfo>();
		    		for(int k=0;k<items2.size();k++){
		    			CategoryInfo cate = new CategoryInfo();
		    			cate.setAmount(items2.get(k).getAmount());
		    			cate.setNum(items2.get(k).getNum());
		    			Double amount = Double.parseDouble(items2.get(k).getAmount());
		    			Double percent = 0.00;
		    			if(shopItemAmount>0&&amount>0){
		    				percent = amount/shopItemAmount;
		    			}
		    			cate.setPercent(df2.format(percent));
		    			cateList.add(cate);
		    		}
		    		items.get(i).setList(cateList);
		    		assignmentAllcateList(allcateList,cateList);
		    	}
			}
			//计算汇总后各个类别的服务项目占百分比
			if(allcateList.size()>0){
				Double servicePencent = 0.00;
				for(int m=0;m<allcateList.size();m++){
					Double seviceAmount = Double.parseDouble(allcateList.get(m).getAmount());
					if(allshopItemResult>0){
						servicePencent = seviceAmount/allshopItemResult;
					}
					allcateList.get(m).setPercent(df2.format(servicePencent));
				}
			}
			allReoprt.setShopName("汇总");
			allReoprt.setShopResult(df1.format(allshopResult));
			allReoprt.setShopItemResult(df1.format(allshopItemResult));
			allReoprt.setShopOutResult(df1.format(allshopOutResult));
			allReoprt.setCashPayAmount(df1.format(allcashPayAmount));
			allReoprt.setCardPayAmount(df1.format(allcardPayAmount));
			allReoprt.setAlipayPayAmount(df1.format(alipayPayAmount));
			allReoprt.setBulkPayAmount(df1.format(allbulkPayAmount));
			allReoprt.setOnlinePayAmount(df1.format(allonlinePayAmount));
			allReoprt.setBalancePayAmount(df1.format(allbalancePayAmount));
			allReoprt.setShopPaidResults(df1.format(shopPaidResults));
			allReoprt.setCustomTotle(allcustomTotle.toString());
			allReoprt.setMcustom(allmcustom.toString());
			allReoprt.setWcustom(allwcustom.toString());
			allReoprt.setHotCustom(allhotCustom.toString());
			allReoprt.setStrangerCustom(allstrangerCustom.toString());
			allReoprt.setList(allcateList);
			items.add(allReoprt);
		}
		return items;
	}
	private void assignmentAllcateList(List<CategoryInfo> all,List<CategoryInfo> list){
		DecimalFormat df1 = new DecimalFormat("#.#");  
		DecimalFormat df2 = new DecimalFormat("#.##%");  
		for(int k=0;k<list.size();k++){
			CategoryInfo cate = new CategoryInfo();
			cate.setAmount(list.get(k).getAmount());
			cate.setNum(list.get(k).getNum());
			cate.setPercent(list.get(k).getPercent());
			if(all.size()>k){
				Double amount = Double.parseDouble(cate.getAmount());
				Integer num = Integer.parseInt(cate.getNum());
				Double percent = Double.parseDouble(cate.getPercent().substring(0, cate.getPercent().length()-1))/100;
				amount+=Double.parseDouble(all.get(k).getAmount());
				num+=Integer.parseInt(all.get(k).getNum());
				percent += Double.parseDouble(all.get(k).getPercent().substring(0, all.get(k).getPercent().length()-1))/100;
				all.get(k).setAmount(df1.format(amount));
				all.get(k).setNum(num.toString());
				all.get(k).setPercent(df2.format(percent));
			}else{
				all.add(cate);
			}
		}
	}

	public List<EmployeeReport> getWageReport(EmployeeReport t) {
		// TODO Auto-generated method stub
		List<EmployeeReport> list = employeeMapper.getWageReport(t);
		EmployeeReport allcount = new EmployeeReport();
		allcount.setEmployeeName("汇总");
		allcount.setEmployeeTitle("");
		BigDecimal allemployeeServiceResults = new BigDecimal(0);
		BigDecimal allemployeeServiceCommission = new BigDecimal(0);
		BigDecimal allemployeeTakeOutResults = new BigDecimal(0);
		BigDecimal allemployeeTakeOutCommission = new BigDecimal(0);
		BigDecimal allamount = new BigDecimal(0);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				String compId = list.get(i).getCompId();
				String employeeId = list.get(i).getEmployeeId();
				Date  beginTime = DateUtil.str2Date(t.getBegDate(), "yyyy-MM-dd HH:mm:ss");
				Date endTime = DateUtil.str2Date(t.getEndDate(), "yyyy-MM-dd HH:mm:ss");
				String  roleId = list.get(i).getRoleId();
				BigDecimal employeeServiceResults = new BigDecimal(0);
				BigDecimal employeeServiceCommission = new BigDecimal(0);
				BigDecimal employeeTakeOutResults = new BigDecimal(list.get(i).getEmployeeTakeOutResults());
				BigDecimal employeeTakeOutCommission = new BigDecimal(list.get(i).getEmployeeTakeOutCommission());
				BigDecimal amount = new BigDecimal(0);
				if(DateUtil.DateDiff(endTime, new Date())<0){
					List<PerformanceHisInfo> pfh = oschService.getPerformanceHisByEmployeeId(compId, employeeId, beginTime, endTime, roleId);
					if(pfh!=null&&pfh.size()>0){
						employeeServiceResults = pfh.get(0).getBaseAmount();
						employeeServiceCommission = pfh.get(0).getCommisionAmount();
						
					}
				}else{
					Map<String,BigDecimal> map = oschService.getPerformanceByEmployeeId(compId, employeeId, roleId);
					employeeServiceResults = map.get("monthAmount");
					employeeServiceCommission = map.get("monthCommisionAmount");
				}
				//计算员提成工总计
				amount=amount.add(employeeServiceCommission);
				amount=amount.add(employeeTakeOutCommission);
				list.get(i).setEmployeeServiceResults(employeeServiceResults.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
				list.get(i).setEmployeeServiceCommission(employeeServiceCommission.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
				list.get(i).setAmount(amount.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
				//计算汇总信息
				
				allemployeeServiceResults=allemployeeServiceResults.add(employeeServiceResults.setScale(1,BigDecimal.ROUND_HALF_UP));
				allemployeeServiceCommission=allemployeeServiceCommission.add(employeeServiceCommission.setScale(1,BigDecimal.ROUND_HALF_UP));
				allemployeeTakeOutResults=allemployeeTakeOutResults.add(employeeTakeOutResults.setScale(1,BigDecimal.ROUND_HALF_UP));
				allemployeeTakeOutCommission=allemployeeTakeOutCommission.add(employeeTakeOutCommission.setScale(1,BigDecimal.ROUND_HALF_UP));
				allamount = allamount.add(amount.setScale(1,BigDecimal.ROUND_HALF_UP));
			}
			allcount.setEmployeeServiceResults(allemployeeServiceResults.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
			allcount.setEmployeeServiceCommission(allemployeeServiceCommission.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
			allcount.setEmployeeTakeOutResults(allemployeeTakeOutResults.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
			allcount.setEmployeeTakeOutCommission(allemployeeTakeOutCommission.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
			allcount.setAmount(allamount.setScale(1,BigDecimal.ROUND_HALF_UP).toString());
			list.add(allcount);
		}
		return list;
	}
}
